//#ifdef __USE_CMSIS
#include "LPC17xx.h"
//#endif
#include "sysinfo.h"
#include "PINCON.h"

#define IER_RBR		0x01
#define IER_THRE	0x02
#define IER_RLS		0x04

#define IIR_PEND	0x01
#define IIR_RLS		0x03
#define IIR_RDA		0x02
#define IIR_CTI		0x06
#define IIR_THRE	0x01

#define LSR_RDR		0x01
#define LSR_OE		0x02
#define LSR_PE		0x04
#define LSR_FE		0x08
#define LSR_BI		0x10
#define LSR_THRE	0x20
#define LSR_TEMT	0x40
#define LSR_RXFE	0x80

void UART1_Init(int baudrate)
{
	int pclk;
	unsigned long int Fdiv;
	int lcr =  0x83; //0b10000011; // 0x83 8bit, 1 stop, no par, disable break, Access to Divisor Latch
	
	UART1PinsEnable();

	// PCLK_UART1 is being set to 1/4 of SystemCoreClock
	pclk = SystemCoreClock / 4;
	// Turn on UART1 peripheral clock
	// A pair of bit in PCLKSEL1 register controls the rate of the clock signal
	// that will be supplied to the corresponding peripheral
	// 00=CCLK/4, 01=CCLK, 10=CCLK/2, 11=CCLK/8 (! CCLK/6 pour CAN)
	LPC_SC->PCLKSEL0 &= ~(3<<8); // met bit9&8 a 0 pour UART1
	LPC_SC->PCLKSEL0 |=  (0<<8); // met bit9&8 a 00 pour avoir une CLK/4
	// Line Control Register p.326

	LPC_UART1->LCR = lcr;
    Fdiv = ( pclk / 16 ) / baudrate ; // Set baud rate
    LPC_UART1->DLM = Fdiv / 256;
    LPC_UART1->DLL = Fdiv % 256;
    LPC_UART1->LCR = 0x03;	// 8 bits, no Parity, 1 Stop bit DLAB=0 -> Disable Access to Divisor Latch
    LPC_UART1->FCR = 0x07;	// Enable and reset TX and RX FIFO
}

// Function to send character over UART
void UART1_Sendchar(char c)
{
	while( (LPC_UART1->LSR & LSR_THRE) == 0 );	// Block until tx empty
	LPC_UART1->THR = c;
}

// Function to get character from UART
char UART1_Getchar()
{
	char c;
	while( (LPC_UART1->LSR & LSR_RDR) == 0 );  // Nothing received so just block
	c = LPC_UART1->RBR; // Read Receiver buffer register
	return c;
}

// Function to prints the string out over the UART
void UART1_PrintString(char *pcString)
{
	int i = 0;
	// loop through until reach string's zero terminator
	while (pcString[i] != 0)
	{
		UART1_Sendchar(pcString[i]); // print each character
		i++;
	}

}



void UART2_Init(int baudrate)
{
	int pclk;
	unsigned long int Fdiv;
	int lcr =  0x83; //0b10000011; // 0x83 8bit, 1 stop, no par, disable break, Access to Divisor Latch
	
	UART2PinsEnable();

	// PCLK_UART2 is being set to 1/4 of SystemCoreClock
	pclk = SystemCoreClock / 4;
	// Turn on UART2 peripheral clock
	// A pair of bit in PCLKSEL1 register controls the rate of the clock signal
	// that will be supplied to the corresponding peripheral
	// 00=CCLK/4, 01=CCLK, 10=CCLK/2, 11=CCLK/8 (! CCLK/6 pour CAN)
	LPC_SC->PCLKSEL0 &= ~(3<<8); // met bit9&8 a 0 pour UART2
	LPC_SC->PCLKSEL0 |=  (0<<8); // met bit9&8 a 00 pour avoir une CLK/4
	// Line Control Register p.326

	LPC_UART2->LCR = lcr;
    Fdiv = ( pclk / 16 ) / baudrate ; // Set baud rate
    LPC_UART2->DLM = Fdiv / 256;
    LPC_UART2->DLL = Fdiv % 256;
    LPC_UART2->LCR = 0x03;	// 8 bits, no Parity, 1 Stop bit DLAB=0 -> Disable Access to Divisor Latch
    LPC_UART2->FCR = 0x07;	// Enable and reset TX and RX FIFO
}

// Function to send character over UART
void UART2_Sendchar(char c)
{
	while( (LPC_UART2->LSR & LSR_THRE) == 0 );	// Block until tx empty
	LPC_UART2->THR = c;
}

// Function to get character from UART
char UART2_Getchar()
{
	char c;
	while( (LPC_UART2->LSR & LSR_RDR) == 0 );  // Nothing received so just block
	c = LPC_UART2->RBR; // Read Receiver buffer register
	return c;
}

// Function to prints the string out over the UART
void UART2_PrintString(char *pcString)
{
	int i = 0;
	// loop through until reach string's zero terminator
	while (pcString[i] != 0)
	{
		UART2_Sendchar(pcString[i]); // print each character
		i++;
	}

}

